﻿using OestsCommon.Model;
using System.Collections.Generic;
using System.Linq;
using Utils;

namespace OestsServer.Net
{
    /// <summary>
    /// tcp通讯类(服务器)
    /// </summary>
    public class ServerController
    {
        #region 根据三种类型的客户端定义三个服务器监听器

        /// <summary>
        /// 平台客户端监听器
        /// </summary>
        internal OESPServerListener OESPServerListener = new OESPServerListener();
        /// <summary>
        /// Unity仿真端监听器
        /// </summary>
        internal UnityServerListener UnityServerListener = new UnityServerListener();
        /// <summary>
        /// 管理系统端监听器
        /// </summary>
       // internal MSServerListener _msServerListener = new MSServerListener();
        #endregion


        #region 单列
        private static ServerController _instance;
        public static ServerController Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ServerController();
                return _instance;
            }
        }

        public ServerController()
        {
            _instance = this;

        }
        #endregion


        public void Start()
        {
            OESPServerListener.Listen();
            UnityServerListener.Listen();
            MyDebug.ServerInfo = $"服务器地址:[{OESPServerListener.IP}],平台端口:[{OESPServerListener.Port}],仿真端口:[{UnityServerListener.Port}]";
            LogNetDebug.LogInfo(MyDebug.ServerInfo);
        }


        public List<IpUser> GetOnlineUsers()
        {
            List<IpUser> oespList = OESPServerListener.GetOnlineUsers();
            List<IpUser> unityList = UnityServerListener.GetOnlineUsers();
            if (oespList == null) return unityList;
            else
            {
                if (unityList != null)
                {
                    foreach (var item in unityList)
                    {
                        if (oespList.Any(x => x.User.DBid == item.User.DBid) == false)
                            oespList.Add(item);
                    }
                }
            }
            return oespList;
        }

        /// <summary>
        /// 强制下线
        /// </summary>
        /// <param name="userId"></param>
        public void ForceOfflineUser(int userId)
        {
            OESPServerListener.LogoffAccount(userId);
            UnityServerListener.LogoffAccount(userId);
        }

        ~ServerController()
        {

        }
    }
}